//
//  YeeCollectionViewModel.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/7.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeCollectionViewModel.h"
#import "YeeDataRequestManger.h"//网络请求
#import "YeeCollectionEntity.h"
@implementation YeeCollectionViewModel
-(void)bindViewModel
{
    [super bindViewModel];
    _command=[[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input)
    {
        RACSignal *requestSignal=[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber)
        {
            NSString *url=@"https://api.douban.com/v2/book/search";
            NSDictionary *parameter=@{@"q":@"基础"};
            [YeeDataRequestManger getJSONWithUrl:url parameters:parameter cache:YES responseCache:^(NSDictionary *responseCache)
             {
                 // 请求成功调用
                 // 把数据用信号传递出去
                 [subscriber sendNext:responseCache];
                 
                 [subscriber sendCompleted];
                
            } success:^(NSDictionary *response)
             {
                 // 请求成功调用
                 // 把数据用信号传递出去
                 [subscriber sendNext:response];
                 
                 [subscriber sendCompleted];
                
            } fail:^(NSError *error)
             {
                //发送错误信息
                 [subscriber  sendError:error];
                 [subscriber   sendCompleted];
            }];
            return [RACDisposable disposableWithBlock:^{
                
            }];
        }];
        return [requestSignal map:^id _Nullable(id  _Nullable value)
         {
             NSArray *array=[YeeCollectionEntity mj_objectArrayWithKeyValuesArray:value[@"books"]];
             return array;
           }];
      }];
}
@end
